<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/3
 * Time: 18:10
 */
class blcakuser extends Captcha{
    //黑名单管理
    function black(){
        global $_W,$_GPC;
        //查询获取用户取消订单的数量
        //查询memeber表 按到用户的id $_W[‘openid’]"
        $openid = $_W['openid'];
        if (empty($openid) || !$openid){
            $openid = $_GPC["openid"];
        }
        if (!$openid || empty($openid)){
            die(json_encode("parms lost"));//参数缺失
        }
        $user_list_sql ="SELECT count(*) FROM ".tablename('tg_order')." WHERE openid = ".$openid." and status =9 and uniacid=".$_W['uniacid'];//获取用户共计取消的订单数
        $error_order = pdo_query($user_list_sql);
        //查询设置
        /*
         * 表名 tg_times
         * 字段 blacktime
         * */
        $black_set = "SELECT * FROM ".tablename("tg_times")." WHERE uniacid = ".$_W['uniacid'];
        $black_setting = pdo_query($black_set);
        $black_num = $black_setting["blacktime"];
        if ($black_set <= $black_num){
            //将用户加入黑名单
            /*
             * 表名 blacklist
             * 字段 openid
             * */
            $data['openid'] = $openid;
            $data['uniacid'] = $_W['uniacid'];
            $res = pdo_insert("blacklist",$data);
            if (!empty($res) || $res){
                //加入黑名单成功
            }else{
                // TODO 如果这里执行失败 只有两种情况 字段不存在和网络环境太差！
            }
        }
    }
    //模板推送 封装一个https请求方法
    function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    //获取所有用户的信息
    function sendtpl(){
        global $_W,$_GPC;
        //获取用户的openid
        $sql = "select * from ".tablename("tg_member")."where uniacid = ".$_W["uniacid"];
        //todo 后期自己写模板框架
        //执行循环写发送方法 用户的openid 字段：from_user
        $data = "";
        $url = "";//模板消息点开的链接跳转
        /*
         *         $tpl = array(
            //接收者 这里面要用到用户的openid
            "touser"=>"",
            //这里是你要发送的模板类型
            "template_id"=>"LikmE1ecqhNMRnqFOBiJdQ9N0FT6ONA_lRiIlRgnZxw",
            //这里是点击模板跳转的页面
            "url" => "www.zhixiaoyuan.net",
            //标题颜色
            "topcolor" => "#FFAE89",
            "data" => array(
//这里的first,keyword 都要对应这模板里面的信息写
                "first" => array("value" => "学生到校通知","color" => "#FF83FA"),
                "keyword1" => array("value" => "名字：小陈","color" => "#FF7536"),
                "keyword2" => array("value" => "班级信息","color" => "#FF69FA"),
                "keyword3" => array("value" => "时间".date('Y-m-d H:i:s'),"color" => "#FF7526"),
//这里是通用的 就类似与备注的存在
                "remark" => array("value" => "谢谢您关注我们的公众号","color" => "#FF7256"),
            ),
        );
        $json_template = json_encode($tpl);
         * */
        //模板发送请求的链接
        $access_token = unserialize($_W["account"]['access_token']);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token".$access_token;
    $res = json_decode($this->https_request($url,$data));
        if ($res["errcode"] == 0){
            echo "chenggong ";
        }else{
            echo json_encode($res);
        }

}